#include "etcder.hpp"
#include "channel.hpp"
#include "base.pb.h"
#include "transmit.pb.h"
#include "user.pb.h"
#include "uuid.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>

DEFINE_bool(log_mode, false, "程序的运行模式: true-发布模式 false-调试模式");
DEFINE_string(log_file, "", "发布模式下，制定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，制定日志的输出等级");

DEFINE_string(etcd_host, "127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监测根目录");

DEFINE_string(service, "/service/transmit_service", "服务名称");

bite_im::ChannelPtr channel;


TEST(用户子服务测试, 文本消息发布和转发1)
{

    bite_im::MsgTransmitService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::NewMessageReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_user_id("3b70-49c7-dfe4-0004");
    req.set_chat_session_id("69d5-5bc3-12ce-0001");
    req.mutable_message()->set_message_type(bite_im::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content("群聊的最后一条消息");
    // req.set_();

    bite_im::GetTransmitTargetRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetTransmitTarget(&cntl, &req, &resp, nullptr);

    ASSERT_TRUE(resp.success());
}

/*TEST(用户子服务测试, 文本消息发布和转发2)
{

    bite_im::MsgTransmitService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::NewMessageReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_user_id("5b54-cf54-dfe7-0005");
    req.set_chat_session_id("74b0-fedb-ffa9-0001");
    req.mutable_message()->set_message_type(bite_im::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content("我也测试一条");
    // req.set_();

    bite_im::GetTransmitTargetRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetTransmitTarget(&cntl, &req, &resp, nullptr);

    ASSERT_TRUE(resp.success());
}
*/


/*TEST(用户子服务测试, 语音消息发布和转发)
{

    bite_im::MsgTransmitService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::NewMessageReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_user_id("ad3a-7782-650d-0000");
    req.set_chat_session_id("会话-1");
    req.mutable_message()->set_message_type(bite_im::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents("speech");
    // req.set_();

    bite_im::GetTransmitTargetRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetTransmitTarget(&cntl, &req, &resp, nullptr);

    ASSERT_TRUE(resp.success());
}*/
/*TEST(用户子服务测试, 图像消息发布和转发)
{

    bite_im::MsgTransmitService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::NewMessageReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_user_id("1f5d-e72e-e845-0001");
    req.set_chat_session_id("会话-1");
    req.mutable_message()->set_message_type(bite_im::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content("image");
    // req.set_();

    bite_im::GetTransmitTargetRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetTransmitTarget(&cntl, &req, &resp, nullptr);

    ASSERT_TRUE(resp.success());
}
*/

/*TEST(用户子服务测试, 文件消息发布和转发)
{

    bite_im::MsgTransmitService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::NewMessageReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_user_id("6771-9495-fdb4-0002");
    req.set_chat_session_id("会话-1");
    req.mutable_message()->set_message_type(bite_im::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_name("测试文件名称");
    req.mutable_message()->mutable_file_message()->set_file_size(5); // 即file字符串的大小
    req.mutable_message()->mutable_file_message()->set_file_contents("file");
    // req.set_();

    bite_im::GetTransmitTargetRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetTransmitTarget(&cntl, &req, &resp, nullptr);

    ASSERT_TRUE(resp.success());
}
*/
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    bite_im::initLogger(FLAGS_log_mode, FLAGS_log_file, FLAGS_log_level);
    auto manager = std::make_shared<bite_im::ServiceManager>();
    manager->declareService(FLAGS_service);
    // 构造服务发现对象
    auto put_cb = std::bind(&bite_im::ServiceManager::onServiceOnline, manager.get(),
                            std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&bite_im::ServiceManager::onServiceOffLine, manager.get(),
                            std::placeholders::_1, std::placeholders::_2);
    auto discoverer = std::make_shared<bite_im::Discovery>(FLAGS_base_service, FLAGS_etcd_host, put_cb, del_cb);

    // 获取服务信道
    channel = manager->choose(FLAGS_service);
    if (channel.get() == nullptr)
    {
        return -1;
    }
    // 我们直接在测试样例中创建Service服务存根

    return RUN_ALL_TESTS();
}